package org.dromara.trace.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.trace.domain.vo.TraceStaffVo;
import org.dromara.trace.domain.bo.TraceStaffBo;
import org.dromara.trace.service.ITraceStaffService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 溯源人员
 *
 * @author Robin
 * @date 2024-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/trace/staff")
public class TraceStaffController extends BaseController {

    private final ITraceStaffService traceStaffService;

    /**
     * 查询溯源人员列表
     */
    @SaCheckPermission("trace:staff:list")
    @GetMapping("/list")
    public TableDataInfo<TraceStaffVo> list(TraceStaffBo bo, PageQuery pageQuery) {
        return traceStaffService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出溯源人员列表
     */
    @SaCheckPermission("trace:staff:export")
    @Log(title = "溯源人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TraceStaffBo bo, HttpServletResponse response) {
        List<TraceStaffVo> list = traceStaffService.queryList(bo);
        ExcelUtil.exportExcel(list, "溯源人员", TraceStaffVo.class, response);
    }

    /**
     * 获取溯源人员详细信息
     *
     * @param staffId 主键
     */
    @SaCheckPermission("trace:staff:query")
    @GetMapping("/{staffId}")
    public R<TraceStaffVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long staffId) {
        return R.ok(traceStaffService.queryById(staffId));
    }

    /**
     * 新增溯源人员
     */
    @SaCheckPermission("trace:staff:add")
    @Log(title = "溯源人员", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TraceStaffBo bo) {
        return toAjax(traceStaffService.insertByBo(bo));
    }

    /**
     * 修改溯源人员
     */
    @SaCheckPermission("trace:staff:edit")
    @Log(title = "溯源人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TraceStaffBo bo) {
        return toAjax(traceStaffService.updateByBo(bo));
    }

    /**
     * 删除溯源人员
     *
     * @param staffIds 主键串
     */
    @SaCheckPermission("trace:staff:remove")
    @Log(title = "溯源人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{staffIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] staffIds) {
        return toAjax(traceStaffService.deleteWithValidByIds(List.of(staffIds), true));
    }
}
